/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.ui;

import java.util.List;

import net.bezrabotnih.client.managed.request.JobTypeProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.place.shared.Place;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * Sample implementation of {@link ProfileView}.
 */
public class ProfileViewImpl extends Composite implements ProfileView {

	interface Binder extends UiBinder<Widget, ProfileViewImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);
	
	private static ProfileView instance;

	private Presenter listener;
	@UiField
	Button editButton;
	@UiField
	SpanElement city;
	@UiField
	SpanElement phoneNumber;
	@UiField
	SpanElement age;
	@UiField
	SpanElement sex;
	@UiField
	SpanElement jobTypes;
	@UiField
	SpanElement name;

	private boolean isEditable=true;
	

	

	private ProfileViewImpl() {
		initWidget(binder.createAndBindUi(this));
	}
	
	public static ProfileView instance(){
		if (instance==null) instance=new ProfileViewImpl();
		return instance;
	}
	
	public static ProfileView create(){
		ProfileView profileView=new ProfileViewImpl();
		profileView.setEditable(false);
		return profileView;
	}

	@Override
	public void setName(String name) {
		this.name.setInnerText(name);
	}
	
	@Override
	public void setCity(String city) {
		this.city.setInnerHTML("<b>Город:</b> "+city);
	}

	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	@UiHandler("editButton")
	void onButtonClick(ClickEvent event) {
		if (isEditable) listener.editClicked();
	}

	@Override
	public void setPhoneNumber(String phoneNumber) {
		if(phoneNumber!=null&&!phoneNumber.equals("")) this.phoneNumber.setInnerHTML("<b>Номер телефона:</b> "+phoneNumber);
		
	}

	@Override
	public void setSex(boolean isMale) {
		 this.sex.setInnerHTML("<b>Пол:</b> "+(isMale?"мужской":"женский"));
		
	}

	@Override
	public void setAge(int age) {
		this.age.setInnerHTML("<b>Возраст:</b> "+age);
		
	}

	@Override
	public void setJobTypes(List<JobTypeProxy> jobTypeProxies) {
		if (jobTypeProxies!=null&&jobTypeProxies.size()>0){
			String jobTypesLabel="<b>Типы выполняемых работ:</b> ";
			for (int i=0;i<jobTypeProxies.size();i++){
				jobTypesLabel+=jobTypeProxies.get(i).getName();
				if (i+1<jobTypeProxies.size()) jobTypesLabel+=", ";
			}
			jobTypes.setInnerHTML(jobTypesLabel);

		}else{
			jobTypes.setInnerHTML("");
		}
	}
	
	public void setEditable(boolean editable){
		this.isEditable=editable;
		editButton.setVisible(editable);
	}
	
	
}
